package com.ygqh.baby.handler;

import com.ygqh.baby.ao.PromotionType;
import com.ygqh.baby.model.PromotionTag;
import com.ygqh.baby.po.YgProductPack;
import com.ygqh.baby.service.YgProductPackDetailService;
import com.ygqh.baby.service.YgProductPackV2Service;
import com.ygqh.baby.utils.DateConvertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/3/8 12:42
 * @since 1.0.0
 */
@Component
public class PackPromotionTagHandler extends AbstractProductPromotionTagHandler implements IProductPromotionTagHandler {


    @Autowired
    private YgProductPackV2Service ygProductPackV2Service;
    @Autowired
    private YgProductPackDetailService ygProductPackDetailService;

    @Override
    protected PromotionTag getPromotionTag(Long relateId) {
        YgProductPack pack = ygProductPackV2Service.findById(relateId);

        PromotionTag tag = new PromotionTag();
        tag.setTagName(pack.getPackPrice().toBigInteger() + "/" + pack.getProductAmount() + "件");
        tag.setResetTime(DateConvertUtils.addDay(pack.getEndDate(), 1));
        tag.setPromotionType(PromotionType.Pack);
        tag.setUpdateBy(pack.getUpdateBy());
        tag.setPromotionId(pack.getId());

        if (pack.getEndDate().getTime() < System.currentTimeMillis()) {
            tag.setExpired(true);
        }
        return tag;
    }

    @Override
    protected void check(Long relateId, List<Long> addList, List<Long> deleteList, Boolean isExpired) {
        if (CollectionUtils.isEmpty(addList) && CollectionUtils.isEmpty(deleteList)) {
            List<Long> productIdsByPackId = ygProductPackDetailService.findProductIdsByPackId(relateId);
            if (isExpired) {
                deleteList.addAll(productIdsByPackId);
            } else {
                addList.addAll(productIdsByPackId);
            }
        }
    }
}
